<?
class FS {
    static protected $cmd_udev = '/sbin/udevadm info -q property -n ';
    var $device;
    var $label;
    var $size;
    var $devtype;
    var $garbirus_mountpoint='/media/garbirus';

    function isMounted() {
        $mounted=exec("grep '".$this->device." ' /proc/mounts && echo $?");
        if (isset($mounted) && $mounted!=''){
            $mounted=(int)$mounted;
            if ($mounted == 0){
                return True;
            } else {
                return False;
            }
        } else {
            return False;
        }
    }

    function getMountpoint(){
        $mountpoint=exec("grep '".$this->device." ' /proc/mounts |cut -d' ' -f2");
        return $mountpoint;
    }

    function mount() {
        $result=exec("/usr/bin/sudo /bin/mount ".$this->device." ".$garbirus_mountpoint);
    }

    function umount() {
        if ($this->isMounted()){
            $result=exec("/usr/bin/sudo /bin/umount ".$this->device);
            return true;
        } else {
            return false;
        }
    }

    function getLabel(){
        $label=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_FS_LABEL='));
        if (isset($label) && count($label)>1){
            $label=$label[1];
        } else {
            $label='<undefined>';
        }
        return $label;
    }

    function getSize(){
        $size=explode('=', exec(self::$cmd_udev.$this->device.'|grep UDISKS_PARTITION_SIZE='));
        if (isset($size) && count($size)>1){
            $size=(int)$size[1];
        } else {
            $size=0;
        }
        return $size;
    }
    
    function getDevtype() {
        $devtype=explode('=', exec(self::$cmd_udev.$this->device.'|grep DEVTYPE='));
        if (isset($devtype) && count($devtype)>1){
            return $devtype[1];
        } else {
            return 'undefined';
        }
    }

    function isFilesystem() {
        $fs=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_FS_USAGE='));
        //echo (self::$cmd_udev.$this->device.'|grep ID_FS_USAGE=');
        if (isset($fs) && count($fs)>1){
            if ($fs[1] == 'filesystem'){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    function FS ($dev) {
        $this->device=$dev;
        $this->devtype=$this->getDevtype();
        if ($this->isFilesystem()){
            $this->label=$this->getLabel();
            $this->size=$this->getSize();
            $this->devtype=$this->getDevtype();
            return true;
        } else {
                //echo "NO FS!!";
                return false;
        }
    }
}
?>
